package com.somlsis.activity

import android.content.Intent
import android.os.CountDownTimer
import android.view.LayoutInflater
import com.base.AppConfig
import com.base.BaseActivity
import com.drake.tooltip.toast
import com.net.HttpResponse
import com.somlsis.R
import com.somlsis.bean.User
import com.somlsis.databinding.ActivityLoginBinding
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class LoginActivity : BaseActivity<ActivityLoginBinding>() {

    private var timer: CountDownTimer? = null
    private var time = 0
    override fun getViewBinding(): ActivityLoginBinding {
        return ActivityLoginBinding.inflate(LayoutInflater.from(this))
    }

    override fun createView() {

        binding.getCode.setOnClickListener {
            val tel = binding.etTel.text.toString().trim()
            if (time != 0) {
                return@setOnClickListener
            }
            if (tel == null || tel != null && tel.length < 11) {
                toast(getString(R.string.toast_tel))
                return@setOnClickListener
            }
            dataCenter.getSMSCode(tel,AppConfig.KEY_LOGIN,object :Callback<HttpResponse<String>>{
                override fun onResponse(
                    call: Call<HttpResponse<String>>,
                    response: Response<HttpResponse<String>>
                ) {

                }

                override fun onFailure(call: Call<HttpResponse<String>>, t: Throwable) {

                }

            })
            countDownTimer()
        }
        binding.login.setOnClickListener {
            val tel = binding.etTel.text.toString().trim()
            val code = binding.etSmsCode.text.toString().trim()
            if (tel == null || tel != null && tel.length < 11) {
                toast(getString(R.string.toast_tel))
                return@setOnClickListener
            }
            if (code == null) {
                toast(getString(R.string.hint_sms_code))
                return@setOnClickListener
            }
            if (!binding.policyCb.isChecked) {
                toast(getString(R.string.clause_toast))
                return@setOnClickListener
            }
            dataCenter.loginForCode(tel, code, object : Callback<HttpResponse<User>> {
                override fun onResponse(
                    call: Call<HttpResponse<User>>,
                    response: Response<HttpResponse<User>>
                ) {
                    if (response.code() == AppConfig.SUCCEED_CODE) {
                        response.body()?.let {
                            dataCenter.setUser(it.data)
                        }
                    }
                    startActivity(Intent(context, BasicInfoActivity::class.java))
                    finish()
                }

                override fun onFailure(call: Call<HttpResponse<User>>, t: Throwable) {
                    toast(t.message)
                }

            })


        }
    }

    private fun countDownTimer() {
        time = 60
        timer = object : CountDownTimer((time + 1) * 1000L, 1000L) {

            override fun onTick(millisUntilFinished: Long) {
                binding.getCode.text = time.toString()
                if (time != 0) {
                    time--
                }else{
                    binding.getCode.text = getString(R.string.get_sms_code)
                }
            }

            override fun onFinish() {

            }
        }

        timer?.start()
    }

    override fun onDestroy() {
        super.onDestroy()
        timer?.let {
            it.cancel()
        }
    }
}